package com.wframe.project.jnd.tag.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.google.gson.Gson;
import com.wframe.project.jnd.services.InformationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.tags.RequestContextAwareTag;

import com.frame.database.JdataBean;
import com.wframe.framework.spring.SpringContextHolder;
import com.wframe.project.jnd.services.CommonService;

import javax.servlet.jsp.PageContext;


public class SysproTag extends RequestContextAwareTag {

    private static final long serialVersionUID = 1L;

    private static final String AD_KEY = "MARK_POSITION_AD_LIST";

    private Map data;

    CommonService commonService;

    @Override
    protected int doStartTagInternal() throws Exception {
        commonService = SpringContextHolder.getBean(CommonService.class);
        data = commonService.getSysPro().getDataMap();
        return 0;
    }

    public int doEndTag() {
        Gson gson = new Gson();
        List<JdataBean> result = null;

        try {
            StringRedisTemplate stringRedisTemplate = SpringContextHolder.getBean(StringRedisTemplate.class);
            if (stringRedisTemplate != null && stringRedisTemplate.opsForValue().get(AD_KEY) != null) {
                String list = StringUtils.trimToEmpty(stringRedisTemplate.opsForValue().get(AD_KEY));
                result = gson.fromJson(list, List.class);
            } else {
                result = commonService.getInformaTion("003201");
                String json = new Gson().toJson(result);
                stringRedisTemplate.opsForValue().set(AD_KEY, json, 10, TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = commonService.getInformaTion("003201");
        }

        pageContext.getRequest().setAttribute("ad_list", result);
        pageContext.getRequest().setAttribute("syspro", data);
        return 0;
    }
}
